package com.cqc.lite.dao;

import com.cqc.lite.domain.Permission;
import com.cqc.lite.domain.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleDao {

    @Select("select * from role where id in(select roleId from users_role where userId=#{userId})")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,
                    many = @Many(select = "com.cqc.lite.dao.PermissionDao.findByRoleId"))
    })
    List<Role> findRolesByUserId(String userId);

    @Select("select * from role")
    List<Role> findAll();

    @Insert("insert role(id,roleName,roleDesc)values(#{id},#{roleName},#{roleDesc})")
    int save(Role role);


    @Select("select * from role where id =#{id}")
    @Results({
            @Result(id = true,property = "id",column = "id"),
            @Result(property = "roleName",column = "roleName"),
            @Result(property = "roleDesc",column = "roleDesc"),
            @Result(property = "permissions",column = "id",javaType = java.util.List.class,
                    many = @Many(select = "com.cqc.lite.dao.PermissionDao.findByRoleId"))
    })
    Role findById(String id);

    @Delete("delete from role_permission where roleId=#{roleId}")
    void deleteRolePermissionByRoleId(String roleId);

    @Insert("insert into role_permission(permissionId,roleId)values(#{permissionId},#{roleId})")
    void insertRolePermission(@Param("permissionId") String permissionId, @Param("roleId")String roleId);
}
